package com.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.domain.Permissions;
import com.core.domain.RolePermissions;
import com.core.mapper.PermissionsMapper;
import com.core.resp.AjaxResponse;
import com.core.resp.PageResult;
import com.core.utils.IdGeneratorSnowFlake;
import com.core.vo.PermissionVo;
import com.core.service.PermissionsService;
import com.core.service.RolePermissionsService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author legion
 * @description 针对表【permissions】的数据库操作Service实现
 * @createDate 2025-06-1 10:44:45
 */
@Service
public class PermissionsServiceImpl extends ServiceImpl<PermissionsMapper, Permissions>
        implements PermissionsService {

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Autowired
    private RolePermissionsService rolePermissionsService;

    @Override
    public PageResult<Permissions> selectPage(Long pageSize, Long pageNum, String description) {
        Long pageStartIndex = (pageNum - 1) * pageSize;
        List<Permissions> permissions = permissionsMapper.selectPage(pageSize, pageStartIndex, description);
        LambdaQueryWrapper<Permissions> queryWrapper = new LambdaQueryWrapper<>();
        if (description != null && !StringUtil.isNullOrEmpty(description)) {
            queryWrapper.eq(Permissions::getDescription, description);
        }
        Long total = permissionsMapper.selectCount(queryWrapper);
        PageResult<Permissions> permissionsPageResult = new PageResult<>(pageSize, pageNum, permissions, total);
        return permissionsPageResult;
    }

    @Override
    public AjaxResponse addPermission(Permissions permissions) {
        try {
            permissions.setId(IdGeneratorSnowFlake.snowFlakeId());
            permissionsMapper.insert(permissions);
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry")) {
                return AjaxResponse.error(405, "权限标识已经存在");
            }
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
        return AjaxResponse.success(200, "添加权限成功", null);
    }

    @Transactional
    @Override
    public AjaxResponse deletePermisson(List<Long> ids) {
        LambdaQueryWrapper<RolePermissions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RolePermissions::getPermissionId, ids);
        List<RolePermissions> list = rolePermissionsService.list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return AjaxResponse.error(405, "有角色与此权限关联");
        }
        permissionsMapper.deleteBatchIds(ids);
        rolePermissionsService.remove(queryWrapper);
        return AjaxResponse.success(200, "删除成功", null);
    }

    @Override
    public void updateById(PermissionVo permissionVo) {
        permissionsMapper.updateDescriptionById(permissionVo);
    }

    @Override
    public Permissions getPermissionByName(String name) {
        LambdaQueryWrapper<Permissions> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Permissions::getName, name);
        return permissionsMapper.selectOne(queryWrapper);
    }
}




